package cn.net.autocode.platform.providers;


import cn.net.autocode.platform.exception.PlatformQueryException;
import cn.net.autocode.platform.providers.service.ProviderServ;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import cn.net.autocode.dbManager.Dao;
import cn.net.autocode.dbManager.handler.PreparedStatementHandler;
import jakarta.annotation.Resource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class ProviderCtrl {

    @Resource(name="dataDao")
    protected Dao dao;

    @Resource(name="dictDao")
    protected Dao dictDao;

    @Resource
    protected ProviderServ providerServ;


    public JSONObject tableMsg(String tableCode) throws PlatformQueryException {
        JSONObject table = dictDao.queryJSON("select * from ac_table where code=?",tableCode);

        if(table==null||table.isEmpty()){
            throw new PlatformQueryException(tableCode+"对应的表不存在");
        }
        return table;
    }


    public String pkTableColumn(String tableName,String columnName){
        JSONArray array = dictDao.queryJSONArray("select table_id,pk_field from ac_table_ref where fk_table=? and fk_field=?",tableName,columnName);
        if (array.isEmpty()){
            return null;
        }else if(array.size()==1){
            return array.getJSONObject(0).getString("pkField");
        }else{
            List<String> tableIds = new ArrayList<>();
            for(int i=0;i<array.size();i++){
                JSONObject json = array.getJSONObject(i);
                tableIds.add(json.getString("tableId"));
            }
            JSONArray jsonArray = dictDao.queryJSONArray("select table_id,count(*) num from ac_table_ref where table_id in (?) and ac_table_ref=? group by table_id",tableIds,tableName);
            String signalTableId = null;
            for(int i=0;i<jsonArray.size();i++){
                JSONObject json = jsonArray.getJSONObject(i);
                if(json.getIntValue("num")==1){
                    signalTableId = json.getString("tableId");
                }
            }

            if(signalTableId==null){
                return null;
            }else{
                for(int i=0;i<array.size();i++){
                    JSONObject json = array.getJSONObject(i);
                    if(signalTableId.equals(json.getString("tableId"))){
                       return json.getString("pkField");
                    }
                }
            }
        }
        return null;
    }




    public String getColumnName(String tableId,String fieldName){
        String selectSql = "select * FROM ac_table_fields where table_id=?";
        JSONArray columns = dictDao.queryJSONArray(selectSql,tableId);

        int cols = columns.size();
        for(int col = 0; col < cols; col++) {
            JSONObject json = columns.getJSONObject(col);
            String columnName = json.getString("name").toLowerCase();
            if(fieldName.equals(PreparedStatementHandler.columnToProperty(columnName))){
                return columnName;
            }
        }
        return null;
    }

    public Map<String,JSONArray> refTables(String tableId,String deleteRule){
        JSONArray refTables;
        if("Y".equals(deleteRule)){
            refTables = dictDao.queryJSONArray("select * from ac_table_ref where table_id=? and delete_rule=? order by fk_table",tableId,deleteRule);
        }else{
            refTables = dictDao.queryJSONArray("select * from ac_table_ref where table_id=? and (delete_rule='N' or delete_rule is null or delete_rule='') order by fk_table",tableId);
        }
        //按table进行分组
        String table = null;
        Map<String,JSONArray> fkTableMaps = new HashMap<>();
        int size = refTables.size();
        for(int i=0;i<size;i++){
            JSONObject refTable = refTables.getJSONObject(i);
            String fkTable = refTable.getString("fkTable");
            if(!fkTable.equals(table)){
                table = fkTable;
                fkTableMaps.put(table,new JSONArray());
            }
            fkTableMaps.get(table).add(refTable);
        }
        return fkTableMaps;
    }

}
